package com.wyx;

import com.wyx.entity.Ems;
import com.wyx.util.ESUtils;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.awt.print.Book;
import java.util.HashMap;
import java.util.Map;

public class test {
    @Test
    public void aa(){
        TransportClient client = ESUtils.getClient();
        Map map = new HashMap<>();
        map.put("name","2222");
        map.put("price",12);
        IndexResponse index = client.prepareIndex("dang", "book", "4").setSource(map).get();
        System.out.println(index);
        client.close();
    }
    @Test
    public void bbb(){
        TransportClient client = ESUtils.getClient();
         SearchResponse searchResponse =client.prepareSearch("dang").setTypes("book")
                        .setQuery(QueryBuilders.multiMatchQuery("qwe","name"))
                                 .get();
        System.out.println(searchResponse);
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit[] hits1 = hits.getHits();
        SearchHit hit = hits1[0];
        String id = hit.getId();
        Map<String, Object> asMap = hit.getSourceAsMap();
        asMap.keySet().forEach(System.out::println);
        Ems ems = new Ems();
        ems.setId(Integer.parseInt(id));
        ems.setName((String) asMap.get("name"));
        ems.setPerice(Double.parseDouble((String) asMap.get("price")));
        System.out.println(ems);
        client.close();
    }
}
